package com.myNetty;

public class scheduleTask implements Runnable,Comparable<scheduleTask>{

    private final Runnable runnable;

    private long deadline;

    public Runnable getRunnable() {
        return runnable;
    }

    public long getDeadline() {
        return deadline;
    }

    public EventLoop getEventLoop() {
        return eventLoop;
    }

    public long getPeriod() {
        return period;
    }

    private long period;

    private EventLoop eventLoop;

    public scheduleTask(Runnable runnable,long deadline,long period,EventLoop eventLoop){
        this.runnable = runnable;
        this.deadline = deadline;
        this.period = period;
        this.eventLoop = eventLoop;
    }

    @Override
    public void run() {
        try {
            runnable.run();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(period > 0){
                deadline += period;
                eventLoop.getScheduleTasks().offer(this);
            }
        }
    }

    @Override
    public int compareTo(scheduleTask o) {
        return Long.compare(this.deadline,o.deadline);
    }
}
